from flask import Flask,request,jsonify
from flask_cors import CORS

app = Flask(__name__)
# CORS(app)  # 启用 CORS 支持

data_store = {
    "messages":["hello,baby","how are you"]
}

@app.route('/api',methods=['GET'])
def get_data():
    return jsonify({
        "status":"success",
        "data":data_store["messages"]
    })

@app.route('/api/add',methods=['POST'])
def add_data():
    request_data = request.get_json()
    if not request_data or 'message' not in request_data:
        return jsonify({
            "status":"error",
            "message":"invalid request"
        }),400
    
    new_message = request_data['message']
    data_store["messages"].append(new_message)

    return jsonify({
        "status":"success",
        "data":new_message
    }),200


# @app.after_request
# def add_cors_headers(response):
#     response.headers['Access-Control-Allow-Origin'] = 'http://localhost:3003'
#     response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
#     response.headers['Access-Control-Allow-Headers'] = 'Content-Type'
#     return response

if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0',port=5000)